package com.empire.module.member.controller.admin.mappolygonpoint;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import com.empire.framework.common.pojo.PageParam;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.pojo.CommonResult;
import com.empire.framework.common.util.object.BeanUtils;
import static com.empire.framework.common.pojo.CommonResult.success;

import com.empire.framework.excel.core.util.ExcelUtils;

import com.empire.framework.apilog.core.annotation.ApiAccessLog;
import static com.empire.framework.apilog.core.enums.OperateTypeEnum.*;

import com.empire.module.member.controller.admin.mappolygonpoint.vo.*;
import com.empire.module.member.dal.dataobject.mappolygonpoint.MapPolygonPointDO;
import com.empire.module.member.service.mappolygonpoint.MapPolygonPointService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 多边形顶点坐标")
@RestController
@RequestMapping("/member/map-polygon-point")
@Validated
public class MapPolygonPointController {

    @Resource
    private MapPolygonPointService mapPolygonPointService;

    @PostMapping("/create")
    @Operation(summary = "创建多边形顶点坐标")
    @PreAuthorize("@ss.hasPermission('member:map-polygon-point:create')")
    public CommonResult<Long> createMapPolygonPoint(@Valid @RequestBody MapPolygonPointSaveReqVO createReqVO) {
        return success(mapPolygonPointService.createMapPolygonPoint(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新多边形顶点坐标")
    @PreAuthorize("@ss.hasPermission('member:map-polygon-point:update')")
    public CommonResult<Boolean> updateMapPolygonPoint(@Valid @RequestBody MapPolygonPointSaveReqVO updateReqVO) {
        mapPolygonPointService.updateMapPolygonPoint(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除多边形顶点坐标")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:map-polygon-point:delete')")
    public CommonResult<Boolean> deleteMapPolygonPoint(@RequestParam("id") Long id) {
        mapPolygonPointService.deleteMapPolygonPoint(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得多边形顶点坐标")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:map-polygon-point:query')")
    public CommonResult<MapPolygonPointRespVO> getMapPolygonPoint(@RequestParam("id") Long id) {
        MapPolygonPointDO mapPolygonPoint = mapPolygonPointService.getMapPolygonPoint(id);
        return success(BeanUtils.toBean(mapPolygonPoint, MapPolygonPointRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得多边形顶点坐标分页")
    @PreAuthorize("@ss.hasPermission('member:map-polygon-point:query')")
    public CommonResult<PageResult<MapPolygonPointRespVO>> getMapPolygonPointPage(@Valid MapPolygonPointPageReqVO pageReqVO) {
        PageResult<MapPolygonPointDO> pageResult = mapPolygonPointService.getMapPolygonPointPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MapPolygonPointRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出多边形顶点坐标 Excel")
    @PreAuthorize("@ss.hasPermission('member:map-polygon-point:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMapPolygonPointExcel(@Valid MapPolygonPointPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MapPolygonPointDO> list = mapPolygonPointService.getMapPolygonPointPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "多边形顶点坐标.xls", "数据", MapPolygonPointRespVO.class,
                        BeanUtils.toBean(list, MapPolygonPointRespVO.class));
    }

}